
var projectPage = 1;
var projectCount = 0;
var domainUrl = "https://www.atu360.cn/";
var homeUrl = "api/phone/index";
var findProjectUrl = "api/phone/findProjectClass?level=0&limit=10";

$(document).ready(function(){
    $.get(domainUrl + homeUrl,function(data){
        carouselPicture(data);
        loadProject();
    });
});

function loadProject(){
    $(document).ready(function(){
        
        $.get(domainUrl + findProjectUrl + "&page=" + projectPage,function(data){
            listProject(data);
        });
    });
    }

//写入轮播链接
function carouselPicture(carouselData) {
    for (var i = 0; i < 5; i++) {
        $("#ADUrl_" + i).attr("href",carouselData.Ad[i].url);
        $("#ADImage_" + i).attr("src",carouselData.Ad[i].image);
    }
}
//列出产品
function listProject(projectData) {
    var projectPages = projectData.pages;
    if (projectPage <= projectPages) {
        for (i = 0; i < projectData.limit; i++ , projectCount++) {
            insertProject(projectCount, projectData);
        }
        projectPage += 1;
    }
    else{
        //移除按钮
        $("#loadMore").remove();
    }
}

//创建新项目
function insertProject(projectCount, projectData) {
    newMedia(projectCount);
    newMediaBody(projectCount);
    newA(projectCount, projectData);
    newTitle(projectCount, projectData);
    newIntroduce(projectCount, projectData);
    newProjectId(projectCount,projectData);
    newImage(projectCount, projectData);
}

//创建 media 容器 div
function newMedia(projectCount) {
    $("#showProject").append("<div id='newMedia_" + projectCount + "'></div>");
    $("#newMedia_" + projectCount).addClass("media border-top p-3");
    $("#newMedia_" + projectCount).css("margin-bottom","5px");
}

//创建 mediabody 容器 div
function newMediaBody(projectCount) {
    $("#newMedia_" + projectCount).append("<div id='newMediaBody_" + projectCount + "'></div>");
    $("#newMediaBody_" + projectCount).addClass("meidia-body");
    $("#newMediaBody_" + projectCount).css("width","80%");
}

//创建项目链接
function newA(projectCount, projectData) {
    $("#newMediaBody_" + projectCount).append("<a id='newA_" + projectCount + "'></a>");
    $("#newA_" + projectCount).css({
        "color" : "black",
        "text-decoration" : "none"
    });
    $("#newA_" + projectCount).attr({
        "href" : projectData.docs[i].url,
        "target" : "_blank"
    });
}

//创建项目标题
function newTitle(projectCount, projectData) {
    $("#newA_" + projectCount).append("<h4 id='newTitle_" + projectCount + "'></h4>");
    $("#newTitle_" + projectCount).text(projectData.docs[i].titleName);
}

//创建项目简介
function newIntroduce(projectCount, projectData) {
    $("#newMediaBody_" + projectCount).append("<p id='newIntroduce_" + projectCount + "'></p>");
    $("#newIntroduce_" + projectCount).text(projectData.docs[i].intro);
}

//加入方案 ID
function newProjectId(projectCount,projectData) {
    $("#newMediaBody_" + projectCount).append("<span id='newProjectId_" + projectCount + "'></span>");
    $("#newProjectId_" + projectCount).text(projectData.docs[i].project_id);
}

// function newProjectLike(projectCount,projectData) {
//     var newProjectLike = document.createElement("a");
//     newProjectLike.id = "newProjectLike_" + projectCount;
//     newProjectLike.type = "button";
//     newProjectLike.className = "btn";
//     newProjectLike.style.display = "inline";
//     document.getElementById("newMediaBody_" + projectCount).appendChild(newProjectLike);
//     document.getElementById("newProject_" + projectCount).insertAdjacentText = "点赞";
// }

//创建项目 Icon
function newImage(projectCount, projectData) {
    $("#newMedia_" + projectCount).append("<img id='newImage_" + projectCount + "'></img>");
    $("#newImage_" + projectCount).attr({
        "src" : projectData.docs[i].icon,
        "alt" : "Block Graph"
    });
    $("#newImage_" + projectCount).addClass("ml-3 mt-3");
    $("#newImage_" + projectCount).css("width","200px");
}

// function newprojectClassify(projectCount,projectData) {
//     var newprojectClassify = document.createElement("a");
//     newprojectClassify.id = "newprojectClassify_" +projectData;
//     newprojectClassify.href = "#";
//     newprojectClassify.style.color = "lightslategray";
//     newprojectClassify.style.display = "inline-block";
// }

// function newLabelContainer(i) {
//     var newLabelContainer = document.createElement("div");
//     newLabelContainer.id = "newLabelContainer_" + i;
//     document.getElementById("hotLabel").appendChild(newLabelContainer);
// }

// function newLabel(i) {
//     var newLabel = document.createElement("a");
//     newLabel.id = "newLabel_" + i;
//     newLabel.className = "btn border rounded";
//     newLabel.target = "_blank";
//     newLabel.href = "#";
//     newLabel.color = "black";
//     newLabel.style.backgroundColor = "darkgray";
//     newLabel.style.marginRight = "10px";
//     document.getElementById("newLabelContainer_" + i).appendChild(newLabel);
// }